<template>
  <div class="top-search clearfix">
    <Input v-model.trim="searchText" class="input-item" />
    <Button @click="getSearch" type="primary" class="button-item" icon="search"></Button>
    <div v-show="boxShow" @blur="divBlur" ref="sBox" tabindex="1" class="search-box">
      <p v-for="item in nodeList" @click="selNode(item)" :key="item.id">{{item.name}}</p>
    </div>
  </div>
</template>

<script>
import {bus} from "./bus";
import {Button, Input, Icon} from 'iview';
export default {
  components: {
    Button, Input, Icon
  },
  data () {
    return {
      searchText: '',
      boxShow: false,
      nodeList: [],
      colors: [
        '#E14716', '#C36729', '#1B94DB', '#CDA91C', '#B3D017', '#5FBB8E', '#46BCCD', '#C4B930'
      ]
    }
  },
  methods: {
    getSearch() {
      var self = this;
      
      if(self.searchText)
      {
        self.boxShow = true;
      }
      else
      {
        return;
      }
      
      setTimeout(function (){
        self.$refs.sBox.focus();
      }, 0);

      self.nodeList = [];
      var list = [];
      for(let i=0; i<10; i++)
      {
        var typeNum = self.getRandom(1, 3);
        var typeName = (typeNum === 1) ? '企业' : '个人';
        
        var relationNum = self.getRandom(1, 3);
        var relationName = (relationNum === 1) ? '股权关系' : '资金关系';

        var obj = {
          id: 'n_' + (i+1),
          name: typeName + '_公司('+ relationName +')_' + (i+1),
          value: self.getRandom(20, 80),
          objType: typeNum,
          color: self.colors[self.getRandom(0, 8)],
          links: {
            source: 'n_' + (i+1),
            target: 'n_' + self.getRandom(1, 11),
            objRelation: relationNum
          }
        }
        
        list.push(obj);
      }
      self.nodeList = list;
    },
    selNode(item) {
      var self = this;
      self.boxShow = false;
      bus.$emit('addNode', item);
    },
    divBlur() {
      var self = this;
      self.boxShow = false;
    }
  },
  mounted () {
    var self = this;
    bus.$on('sendSel', function (data, num){
      data.map(function (item, index){
        var typeNum = self.getRandom(1, 3);
        var typeName = (typeNum === 1) ? '企业' : '个人';
        
        var relationNum = self.getRandom(1, 3);
        var relationName = (relationNum === 1) ? '股权关系' : '资金关系';
        
        var obj = {
          id: 'n_' + (index + num),
          name: item.name,
          value: self.getRandom(20, 40),
          objType: typeNum,
          color: self.colors[self.getRandom(0, 8)],
          links: {
            source: 'n_' + (index + num),
            target: 'n_' + self.getRandom(index, index + num),
            objRelation: relationNum
          }
        }

        bus.$emit('addNode', obj);
      });
    });
  }
}
</script>

<style>
  .top-search .input-item input {
    border: 1px solid #306ADF;
    border-right: none;
    border-radius: 4px 0 0 4px;
  }
  .top-search .button-item .ivu-icon {
    font-size: 20px;
  }
</style>
<style scoped>
  .top-search {
    position: fixed;
    z-index: 1;
    top: 20px;
    left: 20px;
    background: rgba(255, 255, 255, .9);
  }
  .input-item {
    width: 200px;
    float: left;
  }
  .button-item {
    float: left;
    border-radius: 0 4px 4px 0;
    padding: 4px 15px;
  }
  .search-box {
    width: 400px;
    min-height: 30px;
    max-height: 400px;
    border: 1px solid #d7d7d7;
    position: absolute;
    border-radius: 4px;
    top: 40px;
    padding: 10px;
    overflow: auto;
    outline: none;
    background: #fff;
    box-shadow: 0 0 5px rgba(0, 0, 0, .2);
  }
  .search-box p {
    line-height: 30px;
    border-bottom: 1px dashed #ccc;
    cursor: pointer;
    padding: 0 10px;
  }
  .search-box p:hover {
    background: #5cadff;
    color: #fff;
  }
</style>
